import Vue from "vue";
import VueRouter from 'vue-router'
import { Toast } from 'vant';
//2.使用路由
Vue.use(VueRouter);
//3.创建VueRouter的实例
const router = new VueRouter({
    //tips:不想要 #（锚点）就添加下面代码
    mode: 'history',
    //4.配置路由的path和组件
    routes: [
        // 修改密码
        {
            path: "/change",
            name: 'changePassword',
            component: () => import("../views/changePassword.vue"),
        },
        // 更多资料
        {
            path: '/more',
            name: 'More',
            component: () => import("../views/More.vue")
        },
        // 我的发布
        {
            path: '/mysend',
            name: 'mySend',
            component: () => import('../views/mySend.vue')
        },
        // 用户发布
        {
            path: '/usersend',
            name: 'userSend',
            component: () => import('../views/userSend.vue')
        },
        {
            redirect: '/home',
            path: "/",
            name: 'Main',
            component: () => import("../views/Main.vue"),
            children: [
                {
                    path: "/home",
                    name: 'Home',
                    component: () => import("../views/Home.vue"),
                },
                {
                    path: "/find",
                    name: 'Find',
                    component: () => import("../views/Find.vue"),
                },
                {
                    path: "/send",
                    name: 'Send',
                    component: () => import("../views/Send.vue"),
                },
                {
                    path: "/chat",
                    name: 'Chat',
                    component: () => import("../views/Chat.vue"),
                },
                {
                    path: "/user",
                    name: 'User',
                    component: () => import("../views/User.vue"),
                }
            ]
        },
    ]
})

router.beforeEach((to, from, next) => {
    let token = localStorage.getItem('token')
    if (token) {
        next()
    } else {
        if (to.path == '/user') {
            next()
        } else {
            next('/user')
            Toast.success('请先登录!')
        }
    }
})

export default router